﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace CardDataService
{
    /// <summary>
    /// Summary description for CardGameService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CardGameService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        /// <summary>
        /// Shuffles the deck back to the original state.
        /// </summary>
        /// <returns>True if Successful</returns>
        [WebMethod]
        public bool ShuffleDeck()
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                var cards = from card in context.HeldBies
                            where card.PlayerID != 0
                            select card;

                foreach (HeldBy card in cards)
                {
                    card.PlayerID = 0;
                }

                context.SubmitChanges();
            }
            return true;
        }

        /// <summary>
        /// Adds a new player to the list of available players.
        /// </summary>
        /// <param name="PlayerName"></param>
        /// <returns></returns>
        [WebMethod]
        public int AddPlayer(String PlayerName)
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                Player newPlayer = new Player();
                newPlayer.PlayerName = PlayerName;
                context.Players.InsertOnSubmit(newPlayer);
                context.SubmitChanges();
            }
            return 1;
        }

        /// <summary>
        /// Deletes a player by Name
        /// </summary>
        /// <param name="PlayerName"></param>
        /// <returns></returns>
        [WebMethod]
        public bool DeletePlayerByName(String PlayerName)
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                var players =  from player in context.Players
                          where player.PlayerName.ToLower().Equals(PlayerName.ToLower())
                          select player;

                context.Players.DeleteAllOnSubmit(players);

                context.SubmitChanges();
            }
            return true;
        }

        /// <summary>
        /// Deletes a player by ID
        /// </summary>
        /// <param name="PlayerID"></param>
        /// <returns></returns>
        [WebMethod]
        public bool DeletePlayerByID(int PlayerID)
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                var players = from player in context.Players
                              where player.PlayerID.Equals(PlayerID)
                              select player;

                context.Players.DeleteAllOnSubmit(players);

                context.SubmitChanges();
            }
            return true;
        }

        /// <summary>
        /// Transfer ownership of a card.
        /// Ex. 1) The dealer hands the card to player 1.
        /// Ex. 2) Player 1 plays a card on the table. 
        /// </summary>
        /// <param name="CardID"></param>
        /// <param name="PlayerID"></param>
        /// <returns></returns>
        [WebMethod]
        public bool TransferCard(int CardID, int PlayerID)
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                var cards = from heldby in context.HeldBies
                            where (heldby.CardID.Equals(CardID))
                            select heldby;
                foreach (HeldBy card in cards)
	            {
            		 card.PlayerID = PlayerID;
	            }

                context.SubmitChanges();
            }

            return true;
        }

        /*
         * on the consumer side you would convert the image 
            example

            String strBLOBFilePath = @"C:\blue hills.jpg";//Modify this path as needed. 

            //Read jpg into file stream, and from there into Byte array. 
            FileStream fsBLOBFile = new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read); 
            Byte[] bytBLOBData = new Byte[fsBLOBFile.Length]; 
            fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length); 
            fsBLOBFile.Close(); 

            Then you can send bytBLOBData to the your webmethod. 

            You can also check this out. 
            http://www.thecodeproject.com/useritems/dotnet_convertimage.asp 

            Hope this helps. 
         * 
         */
    }
}
